#include <allegro.h>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>
#include "game.h"
#include "actor.h"
#include "actorManager.h"
#include "controllableActor.h"
#include "collisionManager.h"
#include "control.h"
#include "keyboard.h"
#include "sprite.h"
#include "bitmap.h"
#include "pixel.h"
#include "aircraft.h"
#include "star.h"


class TestFrameWork : public Game{
    public:
        void main();
};

void TestFrameWork::main(){
    /*Creo bitmaps que voy a usar para animar la nave y creo el objeto aircraft*/
    BITMAP *bmp1, *bmp2, *bmp3;
    AirCraft *airc=new AirCraft(this);

    /*Creo un control y agrego las acciones para moverse*/
    Control *control_p1=new Control;

    control_p1->add_action_name(AirCraft::DOWN, "Bajar");
    control_p1->add_action_name(AirCraft::UP, "Subir");
    control_p1->add_action_name(AirCraft::LEFT, "Izquierda");
    control_p1->add_action_name(AirCraft::RIGHT, "Derecha");
    control_p1->add_action_name(AirCraft::SHOOT, "Disparar");

    /*Creo un teclado y creo las acciones que van a realizar,
     *con la accion, el periferico, el componente del periferico y el evento del componente*/
    Keyboard *kboard=new Keyboard;
    control_p1->set_actionperipheral(AirCraft::DOWN, kboard, KEY_DOWN,
        Peripheral::ON_PRESSING);
    control_p1->set_actionperipheral(AirCraft::UP, kboard, KEY_UP,
        Peripheral::ON_PRESSING);
    control_p1->set_actionperipheral(AirCraft::LEFT, kboard, KEY_LEFT,
        Peripheral::ON_PRESSING);
    control_p1->set_actionperipheral(AirCraft::RIGHT, kboard ,KEY_RIGHT,
        Peripheral::ON_PRESSING);
    control_p1->set_actionperipheral(AirCraft::SHOOT, kboard ,KEY_SPACE,
        Peripheral::ON_PRESS);

    /*Seteo a quien pertenece el control 1*/
    control_p1->set_owner(airc);
    /*Agrego el control 1 al manager*/
    control_manager->add_control(control_p1);
    /*Agrego el teclado al manager*/
    control_manager->add_peripheral(kboard);

    /*Creo las estrellas y sus representaciones graficas*/
    Star *star_tmp;
    Pixel *pixel_tmp;
    for (int i=0; i<100;i++){
        star_tmp=new Star(this);
        pixel_tmp=new Pixel(star_tmp, makecol(255, 255, 255));
        star_tmp->set_actor_graphic(pixel_tmp);
        star_tmp->set_y(rand()%SCREEN_H);
        star_tmp->set_is_detected(true);
        star_tmp->set_team(ALLY);
        star_tmp->set_collision_method(CollisionManager::PP_COLLISION);
        //Agrego la estrella al manejador de actores
        actor_manager->add(star_tmp);
    }
    /*Creo los bitmaps para la nave*/
    bmp1=load_bitmap("nave01.pcx", NULL);
    bmp2=load_bitmap("nave02.pcx", NULL);
    bmp3=load_bitmap("nave00.pcx", NULL);
    /*Creo un sprite y le agrego los bitmaps*/
    Sprite *sp=new Sprite(airc);
    sp->add_frame(bmp1, bmp1->w/2, bmp1->h/2, 10);
    sp->add_frame(bmp2, bmp2->w/2, bmp2->h/2, 10);
    sp->add_frame(bmp3, bmp3->w/2, bmp3->h/2, 10);
    /*Posiciono la nave en el medio de la pantalla*/
    airc->set_x(SCREEN_W/2);
    airc->set_y(SCREEN_H/2);
    airc->set_actor_graphic(sp); //el sprite va a ser mi actorGraphic
    airc->set_is_detected(true);
    airc->set_team(ENEMY);
    airc->set_collision_method(CollisionManager::PP_COLLISION);
    actor_manager->add(airc); //Agrego la nave al manejador de actores

    while (!key[KEY_ESC]) update();
    //save_bitmap("screen.bmp",screen,tmp);
    destroy_bitmap(bmp1);
    destroy_bitmap(bmp2);
    destroy_bitmap(bmp3);
    delete airc;
    delete sp;
}

int main(){
    TestFrameWork game;
    srand(time(NULL));
    game.set_name("Test del Marco de Trabajo");
    game.init(GFX_AUTODETECT_WINDOWED, 640,480,16);
}
